package com.huihuiyun.huihuiyun.server.controller.member.client;

import com.huihuiyun.huihuiyun.server.controller.member.client.viewobjects.HhyMemberFavouriteVO;
import com.huihuiyun.huihuiyun.server.domain.HhyMemberFavourite;
import com.huihuiyun.huihuiyun.server.service.HhyMemberFavouriteService;
import com.huihuiyun.huihuiyun.server.service.HhyMemberService;
import com.huihuiyun.huihuiyun.server.service.HhyWalletService;
import com.huihuiyun.huihuiyun.server.viewobject.ResultVO;
import com.huihuiyun.huihuiyun.server.viewobject.ResultVOUtil;
import com.mark.util.JaxrsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.List;

@Component
@Path(Const.context)
@Slf4j
public class MemberClientMemberFavouriteController {
    @Context
    private HttpServletResponse httpServletResponse;

    @Autowired
    private HhyMemberService memberService;

    @Autowired
    private HhyWalletService hhyWalletService;

    @Autowired
    private HhyMemberFavouriteService hhyMemberFavouriteService;

    /*******************
     * member favourite
     ******************/
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/findMemberFavouriteListByMemberId")
    public ResultVO<List<HhyMemberFavouriteVO>> findMemberFavouriteListByMemberId(@QueryParam("memberId") String memberId) {
        try {
            List<HhyMemberFavourite> hhyMemberFavouriteList = hhyMemberFavouriteService.findListByMemberId(memberId);
            List<HhyMemberFavouriteVO> hhyMemberFavouriteVOList = ResultVOUtil.convertList(hhyMemberFavouriteList, HhyMemberFavouriteVO.class);
            return ResultVOUtil.success(hhyMemberFavouriteVOList);
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseInternalServerError(httpServletResponse);
            return null;
        }
    }

    @POST
    @Path("/memberFavourite/new")
    @Produces(MediaType.APPLICATION_JSON)
    public ResultVO newBook(HhyMemberFavouriteVO hhyMemberFavouriteVO) {
        try {
            // 业务
            HhyMemberFavourite hhyMemberFavourite = ResultVOUtil.convert(hhyMemberFavouriteVO, HhyMemberFavourite.class);
            hhyMemberFavourite = hhyMemberFavouriteService.post(hhyMemberFavourite);
            hhyMemberFavouriteVO = ResultVOUtil.convert(hhyMemberFavourite, HhyMemberFavouriteVO.class);
            return ResultVOUtil.success(hhyMemberFavouriteVO);
        } catch (Exception e) {
            log.error("", e);
            return ResultVOUtil.error("创建失败，请稍后重试");
        }
    }

    @POST
    @Path("/memberFavourite/delete")
    @Produces(MediaType.APPLICATION_JSON)
    public ResultVO deleteBook(HhyMemberFavouriteVO hhyMemberFavouriteVO) {
        try {
            String id = hhyMemberFavouriteVO.id;
            // 业务
            hhyMemberFavouriteService.delete(id);
            return ResultVOUtil.success();
        } catch (Exception e) {
            log.error("", e);
            return ResultVOUtil.error("删除失败，请稍后重试");
        }
    }
}